/**@author Guy Franco
*This function is a GUI function used by the Supervisor
*It presents the supervisor with a menu for him to pick
**/
package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Entities.ExecutionEntity;

import common.ChatIF;

public class ObserveExecutionPanel extends JPanel implements ChatIF{
	private JTextField textField;
	private AcademicFrame frame;
	private ObserveExecutionPanel observeExecutionPanel;
	private InformationDepartmentManagerPanel informationDepartmentManagerPanel;
	private ViewExecutedPanel viewExecutedPanel;
	
	public ObserveExecutionPanel(AcademicFrame frame, InformationDepartmentManagerPanel myself) {
		super();
		this.frame=frame;
		this.informationDepartmentManagerPanel=myself;
		this.observeExecutionPanel=this;
		frame.setSize(500, 400);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent evt) {
				char k=evt.getKeyChar();
				if (!(k>='0' && k<='9'))
				        evt.consume();
			}
		});
		textField.setBounds(63, 124, 142, 20);
		add(textField);
		textField.setColumns(10);
		initialize();
	}
	
private void initialize() {
		setLayout(null);
		
		JLabel lblObserveAssesments = new JLabel("Observe Executions");
		lblObserveAssesments.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblObserveAssesments.setForeground(Color.BLUE);
		lblObserveAssesments.setBounds(113, 27, 236, 22);
		add(lblObserveAssesments);
		
		JLabel lblChooseAnAssesment = new JLabel("Choose an Executed Request ");
		lblChooseAnAssesment.setBounds(63, 99, 164, 14);
		add(lblChooseAnAssesment);
		
		JButton btnView = new JButton("View");
		btnView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(textField.getText().equals(""))
				{
					(new Dialog("Please Enter an Execution Number to Observe")).setVisible(true);
				}
				else
				{
				ArrayList<Object> exe =new ArrayList<Object>();
				exe.add("getExecute");
				exe.add(textField.getText());
				AcademicFrame.myclient.sendMessageToServer(exe);
				}
				
				
			}
		});
		btnView.setBounds(271, 123, 89, 23);
		add(btnView);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(informationDepartmentManagerPanel);
			}
		});
		btnCancel.setBounds(167, 232, 89, 23);
		add(btnCancel);
	}

		
		


	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub
		ArrayList<Object> answer=new ArrayList<Object>();
		answer.clear(); // clear the info array list
		answer = (ArrayList<Object>)message;
		
		if(answer.get(1).equals("Valid"))
		{
			ExecutionEntity executionEntity = (ExecutionEntity)(answer.get(2));
			
			
			viewExecutedPanel = new ViewExecutedPanel(textField.getText(),frame,observeExecutionPanel);
			frame.setSize(550, 680);
			frame.setContentPane(viewExecutedPanel);
		}
		else
		{
			(new Dialog("Non Existing Assessment")).setVisible(true);
		}
		
	}

}
